//index.js
//获取应用实例
const qiniuUploader = require("../../utils/qiniuUploader");
const app = getApp()

// 初始化七牛相关参数
function initQiniu() {
  var options = {
    region: 'ECN', // 华东区
    uptokenURL: 'https://xcx.guonong8.com/exmp/uptoken.php',
    // uptoken: 'xxxx',
    domain: 'http://oacqzxu7p.bkt.clouddn.com',
    shouldUseQiniuFileName: true
  };
  qiniuUploader.init(options);
}
Page({
  data: {
    imageObject: {},
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    index: 0,
    uploadIcon:false,
    uploadText:'上传图片',
    array: ['一室一厅', '两室一厅','三室两厅'],
    direction_index: 0,
    direction: ['南', '北', '西', '东' ],
    decoration_index:0,
    decoration_style:['简装','精装'],
    leixing_index:0,
    leixing:['普通','复式'],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    items: [
      { name: 'USA', value: '电视' },
      { name: 'CHN', value: '热水器', checked: 'true' },
      { name: 'BRA', value: '空调' },
      { name: 'JPN', value: '冰箱' },
      { name: 'ENG', value: '洗衣机' },
      { name: 'TUR', value: '床' },
      { name: 'TUR1', value: '衣柜' },
      { name: 'TUR1', value: 'wifi' },
    ]
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    wx.showShareMenu({
      withShareTicket: true
    })

    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  bindPickerChange: function (e) {
    this.setData({
      index: e.detail.value
    })
  },
  bindPickerChangeFloor: function (e) {
    this.setData({
      direction_index: e.detail.value
    })
  },
  bindPickerChangeDecoration:function(e) {
    this.setData({
      decoration_index: e.detail.value
    })
  },
  bindPickerChangeLeixing: function (e) {
    this.setData({
      leixing_index: e.detail.value
    })
  },
  checkboxChange:function(e){
    console.log(e.detail.value)
  },
  dotel: function (e) {
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })
  },
  formSubmit: function (e) {
    var that = this;
    var imgurl = that.data.imageObject.imageURL ? that.data.imageObject.imageURL :'';
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
    if (e.detail.value.area == '' || e.detail.value.fangwu == '' || e.detail.value.rent == '' || e.detail.value.louceng == '') {
      wx.showToast({
        title: '数据不能为空!',
        icon: 'loading',
        duration: 1500
      })

      setTimeout(function () {
        wx.hideToast()
      }, 1500)
    } else {
      console.log(e.detail.value.peizhi)
      wx.request({
        url: 'http://www.weshoppma.com/api/house/add_house',
        header: {
          "Content-Type": "application/x-www-form-urlencoded"
        },
        method: "POST",
        data: {
          htitle: e.detail.value.fangtitle,
          fangwu: e.detail.value.fangwu,
          rent: e.detail.value.rent,
          huxing: e.detail.value.huxing,
          direction: e.detail.value.direction,
          area: e.detail.value.area,
          louceng: e.detail.value.louceng,          
          img: imgurl,
          address: e.detail.value.address,
          leixing: e.detail.value.leixing,
          decoration_style: e.detail.value.decoration_style,
          peizhi: e.detail.value.peizhi,
          desc: e.detail.value.textarea,
          contact: e.detail.value.contact,
          tel: e.detail.value.tel,
        },
        success: function (res) {
          if (res.data.status == 0) {
            wx.showToast({
              title: res.data.info,
              icon: 'loading',
              duration: 1500
            })

          } else {
            wx.showToast({
              title: res.data.info,//这里打印出登录成功
              icon: 'success',
              duration: 2000
            });
            wx.reLaunch({
              url: "../../pages/index/index"
            });
          }
        }
      })
    }
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '自定义转发标题',
      success: function (res) {
        // 转发成功
        console.log(res.shareTickets);
        wx.getShareInfo({
          shareTicket: res.shareTickets,
          complete(res) {
            console.log(res)
          }
        })
        // console.log(res.shareTickets[0]);
      },
      fail: function (res) {
        // 转发失败
      }


    }
  },
  didPressChooesImage: function () {
    var that = this;
    that.setData({
      'uploadText': '上传图片中....',
      uploadIcon:true
    });
    didPressChooesImage(that);
    
  }
})

function didPressChooesImage(that) {
  initQiniu();
  // 微信 API 选文件
  wx.chooseImage({
    count: 1,
    success: function (res) {
      var filePath = res.tempFilePaths[0];
      // 交给七牛上传

      qiniuUploader.upload(filePath, (res) => {
        that.setData({
          'imageObject': res,
          'uploadText': '上传图片',
          uploadIcon: false          
        });
        
        console.log(that.data.imageObject);
      }, (error) => {
        console.error('error: ' + JSON.stringify(error));
      });

    }
  })
}